<?php
include("../../tools/session.php");
include("../../tools/controls.php");
include("../../tools/pub.php");

// 如果登录失败、无权限访问，那么采取相应措施
if (!($is_login_success === true)) { session_hop_page("../index.php"); exit(); }
if (!power_check(0)) { include("tpl-inc-wp.php"); $tpl->display($oa_tpl_path . "/no-power.html"); exit(); }

/*-- 以下是实际内容 --*/
$page_status = $_POST["page_status"];
$per_page = 10;
if (!$page_status)
{
   include("../../tools/mysql.php");
   db_connect();
   include("tpl-inc-wp.php");
   
   // $sql = "SELECT ceil(count(plan_id)/" . $per_page . ") num FROM workplan";
   $sql = "SELECT count(plan_id) num FROM workplan WHERE author_id=" . $_SESSION["user"]["id"];
   $re = db_query_once($sql);
   
   // $tpl->assign("page_num", $re["num"]);
   $tpl->assign("wholepage", $re["num"]);
   $tpl->display($oa_tpl_path . "/default/plan.html");
}
// 如果 $page_status 为 1, 则显示 XML 数据
elseif ($page_status == 1)
{
   include("../../tools/mysql.php");
   db_connect();
   
   header("content-type: text/xml");
   echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
   echo "<plans>\n";
   
   $page = $_POST["page"];
   $page = ($page == "" || $page <= 0) ? 1 : $page;
   
   $sql = "SELECT plan_id, plandate, title, content, applydate, is_finished "
	 . " FROM workplan WHERE author_id=" . $_SESSION["user"]["id"] . " ORDER BY applydate DESC";
   $re = db_query($sql);
   
   foreach ($re as $v)
	 {
		$plandate = explode(" ", $v["plandate"]);
		$applydate = explode(" ", $v["applydate"]);
		echo "<item>\n";
		echo "<id>" . $v["plan_id"] . "</id>\n";
		echo "<plandate>" . $plandate[0] . "</plandate>\n";
		echo "<title><![CDATA[" . $v["title"] . "]]></title>\n";
		echo "<content><![CDATA[" . $v["content"] . "]]></content>\n";
		echo "<applydate><![CDATA[" . $applydate[0] . "]]></applydate>\n";
		echo "<isfinished>" . $v["is_finished"] . "</isfinished>\n";
		echo "</item>\n";
	 }
   
   echo "</plans>\n";
}
// 如果 $page_status 为 2
elseif ($page_status == 2)
{
   include("../../tools/mysql.php");
   db_connect();
   
   $pdate = $_POST["pdate"];
   $ptitle = mysql_escape_string($_POST["ptitle"]);
   $pcontent = mysql_escape_string($_POST["pcontent"]);
   
   $sql = "INSERT INTO workplan(plandate, title, content, author_id, applydate, is_finished) "
	 . " VALUES('" . $pdate . "', '" . $ptitle . "', '" . $pcontent . "', "
	 . $_SESSION["user"]["id"] . ", NOW(), 0)";
	 
   $log_str = array("发布新的计划：“" . $ptitle . "”", "发布新计划失败！");
   echo check_status_write_to_log(db_exec($sql), $log_str, FALSE);
}
// 如果 $page_status 为 3, 删除
elseif ($page_status == 3)
{
   include("../../tools/mysql.php");
   db_connect();
   
   $id = $_POST["id"];
   $sql = "DELETE FROM workplan WHERE plan_id=" . $id;
   $log_str = array("删除工作计划（ID: " . $id . "）成功！", "删除工作计划（ID: " . $id . "）失败！");
   echo check_status_write_to_log(db_exec($sql), $log_str, FALSE); 
}
// 如果 $page_status 为 4，切换完成状态
elseif ($page_status == 4)
{
   include("../../tools/mysql.php");
   db_connect();
   
   $id = $_POST["id"];
   $check = $_POST["check"];
   
   $sql = "UPDATE workplan SET is_finished=" . $check . " WHERE plan_id=" . $id;
   $log_str = array("切换工作计划（ID: " . $id . "）状态成功！", "切换工作计划（ID: " . $id . "）状态失败！");
   echo check_status_write_to_log(db_exec($sql), $log_str, FALSE);
}
// 如果 $page_status 为 5，显示计划预览 XML
elseif ($page_status == 5)
{
   $id = $_POST["id"];
   
   include("../../tools/mysql.php");
   db_connect();
   
   header("content-type: text/xml");
   echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
   echo "<plan>\n";
   
   $page = $_POST["page"];
   $page = ($page == "" || $page <= 0) ? 1 : $page;
   
   $sql = "SELECT title, content "
	 . " FROM workplan WHERE author_id=" . $_SESSION["user"]["id"]
	 . " AND plan_id=" . $id . " ORDER BY applydate DESC";
   $re = db_query($sql);
   
   foreach ($re as $v)
	 {
		echo "<title><![CDATA[" . $v["title"] . "]]></title>\n";
		echo "<content><![CDATA[" . $v["content"] . "]]></content>\n";
	 }
   
   echo "</plan>\n";
}

?>
